//
//  BKXianXiaShengQingView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/24.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BKXianXiaShengQingView.h"
#import "SYRequestAPI+FuDao.h"

@interface BKXianXiaShengQingView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) SYNoLabel *nolab;
@property (nonatomic, strong) NSMutableArray *listDataArr;
@end

@implementation BKXianXiaShengQingView

- (void)viewDidLoad {
    [super viewDidLoad];
    _nolab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self.view];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    pageSize = 20;
    pageIndex = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@30);
        make.right.equalTo(@0).offset(-30);
        make.top.equalTo(@30);
        make.bottom.equalTo(@0);
    }];
//    [_tableList headerRefreshBlock:^{
//        [self getXianXiaFuDaoRequest];
//    }];
//    [_tableList fooderRefreshBlock:^{
//        [self getXianXiaFuDaoRequest];
//    }];

}
- (void)requestDataArr:(NSMutableArray *)listArr {
    if (listArr.count == 0) {
        _nolab.hidden = NO;
        self.tableList.hidden = YES;
    } else {
        self.tableList.hidden = NO;
        self.listDataArr = listArr;
        [self.tableList reloadData];
    }
}

- (void)layout{
    
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(30, 0, self.view.frame.size.width-60, APP_HEIGHT-84) style:UITableViewStyleGrouped];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        [tableView registerClass:[BKXXSQTableViewCell class] forCellReuseIdentifier:@"BKXXSQTableViewCell"];
        [tableView registerClass:[XianXiaShenQingInClassCell class] forCellReuseIdentifier:@"XianXiaShenQingInClassCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    XianXiaShengQingAllModel *model = _listDataArr[section];
    
    return model.dataLesson.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _listDataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XianXiaShengQingAllModel *model = _listDataArr[indexPath.section];
    DataLessonModel *lessonModel = model.dataLesson[indexPath.row];
    if (GetTheLatestDate(model.attendDate)) {
        //yes能报名
        //线下申请就要开始的课程班级
        XianXiaShenQingInClassCell *cell =[tableView dequeueReusableCellWithIdentifier:@"XianXiaShenQingInClassCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[XianXiaShenQingInClassCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XianXiaShenQingInClassCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row%2 == 0) {
            cell.backgroundColor = [UIColor whiteColor];
        }else{
            cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
        }
        [cell setStartLabelValue: [NSString stringWithFormat:@"%@",lessonModel.beginDate]];
        cell.block = ^(NSIndexPath * _Nonnull indexPath) {
            [self.manDelegate BKXianXiaShengQingViewAddDianMing:lessonModel];
        };
        [cell setApplayNumberLabelValue:lessonModel.applyNums];
        return cell;

    }else{
        //不能报名
        BKXXSQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BKXXSQTableViewCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[BKXXSQTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BKXXSQTableViewCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row%2 == 0) {
            cell.backgroundColor = [UIColor whiteColor];
        }else{
            cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
        }
        [cell setStartLabelValue:[NSString stringWithFormat:@"%@",lessonModel.beginDate]];
        [cell setApplayNumberLabelValue:lessonModel.applyNums];
        [cell setEndLabelValue:@"20"];
        cell.checkBlock = ^(NSIndexPath *indexPath) {
            [self.manDelegate BKXianXiaShengQingViewAddCheck:lessonModel];
        };
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   // NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    XianXiaShengQingAllModel *model = _listDataArr[section];
    _titleView = [[BKXXSQHeaderView alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width-60, 44)];
    _titleView.backgroundColor = CONTENTBACKCOLOR;
    _titleView.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
    _titleView.leftLabel.attributedText = [PublickFunctionObject getPriceAttribute:[NSString stringWithFormat:@"日期:%@%@",model.attendDate,model.weekName] and:SELECTEDCOLOR andChangeBenginString:@":" andEndString:@"" andSize:14];
    _titleView.centerLabel.attributedText = [PublickFunctionObject getPriceAttribute:[NSString stringWithFormat:@"教室:%@",model.classroomName] and:SELECTEDCOLOR andChangeBenginString:@":" andEndString:@"" andSize:14];
    _titleView.rightLabel.attributedText = [PublickFunctionObject getPriceAttribute:[NSString stringWithFormat:@"容纳人数:%@",model.maxPerson] and:SELECTEDCOLOR andChangeBenginString:@":" andEndString:@"" andSize:14];
    return _titleView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    mainView.frame = CGRectMake(0, 0, self.view.frame.size.width, 30);
    return mainView;
}
BOOL GetTheLatestDate(NSString *stringFirst){
    NSString *current = [PublickFunctionObject getTimeSpan:@"YYYY-MM-DD"];
    NSString *fist = [PublickFunctionObject timeSwitchTimestamp:stringFirst andFormatter:@"YYYY-MM-DD"];
    if ([current floatValue]>[fist floatValue]) {
        return NO;
    }else{
        return YES;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
